package com.zhuhanpeng.cloud.login.service.impl;

import com.zhuhanpeng.cloud.login.repository.LoginDao;
import com.zhuhanpeng.cloud.login.service.LoginService;
import com.zhuhanpeng.cloud.mybatis.entity.PtMenuEntity;
import com.zhuhanpeng.cloud.mybatis.entity.PtUserEntity;
import com.zhuhanpeng.cloud.spring.entity.GdmtUserEntity;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Console;
import java.util.ArrayList;
import java.util.List;

@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    private LoginDao loginDao;


    @Override
    public PtUserEntity queryUserInfoByName(String username) {
        return loginDao.queryUserInfoByName(username);
    }

    @Override
    public List<PtMenuEntity> queryMenuInfoList() {
      GdmtUserEntity userEntity=(GdmtUserEntity) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
              String   lange=LocaleContextHolder.getLocale().toString();
        List<PtMenuEntity> ptMenuEntityList= loginDao.queryMenuInfoListByUuidLang(userEntity.getUserUuid(),lange);
        List<PtMenuEntity>  ptMenuLevelList=new ArrayList<>();

        Boolean findlag=false;
        for(PtMenuEntity item:ptMenuEntityList){
            findlag=false;
            for(PtMenuEntity itemParent:ptMenuEntityList){
                if(item.getParentId().equals(itemParent.getMenuId())){
                    itemParent.addChildren(item);
                    findlag=true;
                    break;
                }
            }
            if(!findlag){
                ptMenuLevelList.add(item);
            }
        }



        return   ptMenuLevelList;
    }
    @Override
    public List<String> queryAuthList(String userUuid) {
//        GdmtUserEntity userEntity= (GdmtUserEntity) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return loginDao.queryAuthList(userUuid);
    }
}
